package services

import (
	"context"
	"errors"
	"fmt"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"log"
	"ltbz/cultural_tourism_api/utils"
	"os"
	"time"
)

func GetMinIOSTSToken() (item *Token, err error) {
	// MinIO服务的访问信息
	item = new(Token)
	item.AccessKeyId = utils.MinIoAccessKeyId
	item.SecretKeyId = utils.MinIoAccessKeySecret
	item.Endpoint = utils.MinIoEndpoint
	item.ImgHost = utils.MinIoImghost
	item.Bucketname = utils.MinIoBucketname
	item.UseSSL = utils.MinIoUseSSL
	item.RegionId = utils.MinIoRegion
	item.Port = utils.MinIoPort
	return
}

type Token struct {
	AccessKeyId string
	SecretKeyId string
	RegionId    string
	Bucketname  string
	Endpoint    string
	ImgHost     string
	UseSSL      string
	Port        string
}

func UploadMinIo() {
	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false

	// Initialize minio client object.
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
	}

	// Make a new bucket called mymusic.
	bucketName := "etastatic"
	location := "/"

	err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
	if err != nil {
		// Check to see if we already own this bucket (which happens if you run this twice)
		exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
		if errBucketExists == nil && exists {
			log.Printf("We already own %s\n", bucketName)
		} else {
			log.Fatalln(err)
		}
	} else {
		log.Printf("Successfully created %s\n", bucketName)
	}
	//buckets, err := minioClient.ListBuckets(ctx)
	//for _, bucket := range buckets {
	//	fmt.Println(bucket)
	//}
	// Upload the zip file
	objectName := "1111.xlsx"
	filePath := "/Users/xi/Desktop/1111.xlsx"
	contentType := "application/xlsx"

	// Upload the zip file with FPutObject
	info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
}

// UploadImgToMinIo 图片上传
func UploadImgToMinIo(fileName, filePath string) (string, error) {
	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
		return "0", errors.New("MinIo信息未配置")
	}

	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false
	if utils.MinIoUseSSL == "true" {
		useSSL = true
	}
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
		return "", err
	}
	bucketName := utils.MinIoBucketname
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		log.Printf("We already own %s\n", bucketName)
	} else {
		log.Fatalln(err)
		return "", err
	}
	path := utils.MinIoUploadDir + time.Now().Format("200601/20060102/")
	path += fileName
	// Upload the zip file with FPutObject
	//contentType := "application/xlsx"
	_, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
	if err != nil {
		log.Fatalln(err)
	}

	path = utils.MinIoImghost + path
	fmt.Println("path:" + path)
	return path, err
}

// UploadAudioToMinIo 音频上传
func UploadAudioToMinIo(fileName, filePath string) (string, error) {
	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
		return "0", errors.New("MinIo信息未配置")
	}

	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false
	if utils.MinIoUseSSL == "true" {
		useSSL = true
	}
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
		return "1", err
	}
	bucketName := utils.MinIoBucketname
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		log.Printf("We already own %s\n", bucketName)
	} else {
		log.Fatalln(err)
		return "2", err
	}

	path := utils.MinIoUpload_Audio_Dir + time.Now().Format("200601/20060102/")
	path += fileName

	// Upload the zip file with FPutObject
	//contentType := "application/xlsx"
	_, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
	if err != nil {
		log.Fatalln(err)
		return "3", err
	}

	path = utils.MinIoImghost + path
	return path, err
}

// UploadVideoToMinIo 视频上传
func UploadVideoToMinIo(filename, filePath, savePath string) error {
	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
		return errors.New("MinIo信息未配置")
	}
	defer func() {
		os.Remove(filePath)
	}()

	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false
	if utils.MinIoUseSSL == "true" {
		useSSL = true
	}
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
		return err
	}
	bucketName := utils.MinIoBucketname
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		log.Printf("We already own %s\n", bucketName)
	} else {
		log.Fatalln(err)
		return err
	}

	//path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
	//path += filename
	_, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
	if err != nil {
		log.Fatalln(err)
		return err
	}
	//path = utils.Imghost + path
	//return path,err
	return err
}

// UploadFileToMinIo 上传文件
func UploadFileToMinIo(filename, filePath, savePath string) error {
	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
		return errors.New("MinIo信息未配置")
	}
	defer func() {
		os.Remove(filePath)
	}()
	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false
	if utils.MinIoUseSSL == "true" {
		useSSL = true
	}
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
		return err
	}
	bucketName := utils.MinIoBucketname
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		log.Printf("We already own %s\n", bucketName)
	} else {
		log.Fatalln(err)
		return err
	}
	//path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
	//path += filename
	_, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
	if err != nil {
		log.Fatalln(err)
		return err
	}
	//path = utils.Imghost + path
	//return path,err
	return err
}

// UploadMinIoToDir 上传至hzchart
func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
		return "0", errors.New("MinIo信息未配置")
	}
	ctx := context.Background()
	endpoint := utils.MinIoEndpoint
	accessKeyID := utils.MinIoAccessKeyId
	secretAccessKey := utils.MinIoAccessKeySecret
	useSSL := false
	if utils.MinIoUseSSL == "true" {
		useSSL = true
	}
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
		return "1", err
	}
	bucketName := utils.MinIoBucketname
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		log.Printf("We already own %s\n", bucketName)
	} else {
		log.Fatalln(err)
		return "2", err
	}
	if uploadDir == "" {
		uploadDir = utils.MinIoUploadDir
	}
	if fileDir == "" {
		fileDir = time.Now().Format("200601/20060102/")
	}
	path := uploadDir + fileDir
	path += filename
	_, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
	if err != nil {
		log.Fatalln(err)
		return "3", err
	}
	path = utils.MinIoImghost + path
	return path, err
}
